package com.hejing.cloud.exp;

import com.hejing.cloud.resp.ResultData;
import com.hejing.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.hejing.cloud.exp
 * Description:
 *      全局异常处理类
 * @Author: HeJing
 * @Create: 2024-08-03 12:56
 * @Version: 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)  //捕捉 RuntimeException异常
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)  // 内部服务器错误
    public ResultData<String> exception(Exception e) {

        System.out.println("###### come in GlobalExceptionHandler");
        log.error("全局异常信息: {}", e.getMessage(), e);

        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }
}




























